package sunwul.mall.commoncore.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;

/**
 * @author sunwul
 * @date 2024/12/27 15:02
 * @description redis缓存配置
 */
@Configuration
public class RedisCacheConfig {

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration() {
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();

        redisCacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair
                .fromSerializer(RedisSerializer.json())) // 值序列化方式为JSON格式
                .entryTtl(Duration.ofDays(7)) // 统一过期时间 7天
                .disableCachingNullValues(); // value值禁止使用空值

        return redisCacheConfiguration;
    }
}
